﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace FoundationLib.Ifc.Authorisation.Configuration
{
	/// <summary>
	/// Represents an authorisation configuration section
	/// </summary>
	public class AuthorisationConfigCollection : ConfigurationElementCollection, IEnumerable<AuthorisationConfigElement>
	{
		/// <summary>
		/// Creates a new instance.
		/// </summary>
		public AuthorisationConfigCollection()
			: base()
		{
		}

		/// <summary>
		/// Gets an element from the collection.
		/// </summary>
		/// <param name="name">The element name</param>
		/// <returns>The element with the specified name of null if no such element exists</returns>
		public new AuthorisationConfigElement this[string name]
		{
			get
			{
				return (this.Where(x => x.Name == name).FirstOrDefault());
			}
		}

		/// <summary>
		/// Gets an element from the collection.
		/// </summary>
		/// <param name="index">The zero-based index of the element</param>
		/// <returns>The element at the specified index</returns>
		public AuthorisationConfigElement this[int index]
		{
			get
			{
				return (this.BaseGet(index) as AuthorisationConfigElement);
			}
		}

		/// <summary>
		/// Gets a strongly-typed collection enumerator.
		/// </summary>
		/// <returns>The collection enumerator</returns>
		public new IEnumerator<AuthorisationConfigElement> GetEnumerator()
		{
			for (int i = 0; i < this.Count; i++)
				yield return this[i];
		}

		/// <summary>
		/// Creates a new element
		/// </summary>
		/// <returns>The element</returns>
		protected override ConfigurationElement CreateNewElement()
		{
			return (new AuthorisationConfigElement());
		}

		/// <summary>
		/// Returns the element key
		/// </summary>
		/// <returns>The key</returns>
		/// <param name="element"></param>
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((AuthorisationConfigElement)element).Name;
		}
	}
}
